<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/8/23
 * Time: 19:24
 */

namespace Biz\service\activity;

use Biz\module\Classes\mgt\MealSave as clsMgtMealSave;
use Biz\model\activity\ActivityMeal as mActivityMeal;
use Biz\model\activity\ActivityMealInfo as mActivityMealInfo;
use Biz\service\Sku as sSku;
use Biz\service\Goods as sGoods;
use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Biz\model\GoodsSku as mGoodsSku;
use Biz\cache\Meal as cMeal;
use Biz\cache\MealInfo as cMealInfo;
use Biz\service\Share as sShare;
use Biz\service\ShopInfo as sShopInfo;
use Biz\tools\BizUrl as tBizUrl;
use Biz\tools\Link as tLink;
/**
 * @deprecated
 */
class MealInfo
{
    const NOT_SURE_REPLACE = 0;
    const SURE_REPLACE = 1;

    const MIN_GROUP_COUNT = 1;
    const MAX_GROUP_COUNT = 2;

    const TYPE_ALL = 0;
    const TYPE_APP = 1;
    const TYPE_H5 = 2;

    const CACHE_TYPE_GOODS = 'goods'; // 商品相关缓存key
    const CACHE_TYPE_SHOP = 'shop'; // 商家相关缓存key
    const CACHE_TYPE_MEAL = 'meal'; // default

    private static $mealField = ['meal_id', 'cover_img', 'meal_title', 'meal_desc', 'meal_img_info', 'tags', 'shop_id', 'start_time', 'end_time'];
    private static $mealInfoField = ['id', 'meal_id', 'goods_id', 'sku_id', 'group', 'meal_rebate', 'start_time', 'end_time'];

    /*
     * 根据商品goods_id缓存套餐详细信息
     * **/
    private static function getMealInfoCacheByGoodsId($goodsId, $type = self::CACHE_TYPE_GOODS)
    {
        $key = cMealInfo::getMealKey($goodsId, $type);
        $goodsMealInfo = cMealInfo::getItem($key);
        if($goodsMealInfo === false) {
            $goodsMealInfo = mActivityMealInfo::getMealInfoByGoodsId($goodsId, self::$mealInfoField);
            $expire = self::getCacheExpire($goodsMealInfo);
            cMealInfo::setItem($key, $goodsMealInfo, $expire);
        }
        return $goodsMealInfo;
    }

    /*
     * 根据shop_id存储缓存套餐基础信息
     * **/
    private static function getMealInfoCacheByShopId($shopId, $type = self::CACHE_TYPE_SHOP)
    {
        $key = cMeal::getMealKey($shopId, $type);
        $mealList = cMeal::getItem($key);
        if($mealList === false) {
            $mealList = mActivityMeal::getMealListByShopId($shopId, self::$mealField);
            $expire = self::getCacheExpire($mealList);
            cMeal::setItem($key, $mealList, $expire);
        }
        return $mealList;
    }

    /*
     * 根据多个套餐id缓存套餐详细信息
     * **/
    private static function getMealInfoCacheByMealIds($mealIds)
    {
        list($mealInfos, $noCacheKey) = cMealInfo::mGetItems($mealIds);
        if($noCacheKey) {
            // 根据套餐id查询对应的商品信息
            $mealInfo = mActivityMealInfo::getMealInfoByMealIds($noCacheKey, self::$mealInfoField);
            foreach ($mealInfo as $info) {
                $nMealInfos[$info['meal_id']][] = $info;
            }
            cMealInfo::mSetItems($nMealInfos);
            $mealInfos = $mealInfos + $nMealInfos;
        }
        return $mealInfos;
    }

    /*
     * 根据单个套餐id缓存套餐详细信息
     * **/
    private static function getMealInfoCacheByMealId($mealId)
    {
        $mealInfo = cMealInfo::getItem($mealId); // 根据套餐id查询对应的商品信息
        if($mealInfo === false) {
            $mealInfo = mActivityMealInfo::getMealInfoByMealIds($mealId, self::$mealInfoField);
            $expire = self::getCacheExpire($mealInfo);
            cMealInfo::setItem($mealId, $mealInfo, $expire);
        }
        return $mealInfo;
    }

    /*
     * 根据多个套餐id缓存套餐基础信息
     * **/
    private static function getMealCacheByMealIds($mealIds)
    {
        list($meal, $noCacheMealKey) = cMeal::mGetItems($mealIds);
        if($noCacheMealKey) {
            $nMeal = mActivityMeal::getMealByMealIds($mealIds, self::$mealField);
            cMeal::mSetItems($nMeal);
            $meal = $meal + $nMeal;
        }
        return $meal;
    }

    /*
     * 计算最近的套餐过期时间
     * **/
    private static function getCacheExpire($meal)
    {
        $meal = reset($meal);
        return $meal ? strtotime($meal['end_time']) - time() : cMeal::_CACHE_EXPIRE_TIME;
    }

    /*
     * 商家后台编辑套餐
     * **/
    public static function mgtSaveMeal($mealId, $post)
    {
        $result = mActivityMeal::action(function () use ($mealId, $post) {

            $result = clsMgtMealSave::update($mealId, $post);

            return $result;
        });

        return $result;
    }

    /*
     * 商家后台修改套餐状态
     * **/
    public static function changeStatus($mealId, $status, $meal)
    {
        $result = mActivityMeal::action(function () use ($mealId, $status, $meal) {

            $result = clsMgtMealSave::changeStatus($mealId, $status, $meal);

            return $result;
        });

        return $result;
    }

    /*
     * 置顶与取消置顶
     * **/
    public static function mgtSetTop($mealId, $isSetTop)
    {
        return clsMgtMealSave::setTop($mealId, $isSetTop);
    }

    /*
     * 商家后台套餐列表
     * **/
    public static function mgtGetMealList($shopId, $page = 1, $pageSize = 20)
    {
        $limit = [($page - 1) * $pageSize, $pageSize];
        $activityMeal = mActivityMeal::admGetActivityMealByShopId($shopId, $limit,true);

        if (!$activityMeal) {
            return [];
        }

        $mealIds = array_column($activityMeal, 'meal_id');
        // 根据套餐id查询对应的商品信息
        $mealInfo = mActivityMealInfo::admGetMealInfoByMealId($mealIds);

        foreach ($mealInfo as $info) {
            $activityMeal[$info['meal_id']]['meal_info'][$info['group']][] = $info;
        }

        foreach ($activityMeal as $info) {
            self::getExtendInfoList($info);
            $list[$info['meal_id']] = $info;
        }

        $activityList = self::mgtFormatMealList($list);
        $mealList['list'] = $activityList ? array_values($activityList) : [];
        $mealList['count'] = mActivityMeal::admGetActivityMealCountByShopId($shopId);
        return $mealList;
    }

    /*
     * 商家后台查询套餐详细信息
     * **/
    public static function mgtGetMealInfo($mealId, $shopId)
    {
        // 查询套餐
        $activityMeal = mActivityMeal::admGetActivityMealById($mealId, true);
        if (!$activityMeal || $activityMeal['shop_id'] != $shopId) {
            throw new \Exception('套餐不存在');
        }

        // 根据套餐id查询对应的商品信息
        $mealInfo = mActivityMealInfo::admGetMealInfoByMealId($mealId);
        foreach ($mealInfo as $info) {
            $activityMeal['meal_info'][$info['group']][] = $info;
        }

        // 拼装sku select的信息
        $activityMeal = self::getExtendInfo($activityMeal);

        return $activityMeal;
    }

    /*
     * 后台获取套餐基础信息
     * **/
    public static function getMeal($mealId)
    {
        $meal = mActivityMeal::admGetActivityMealById($mealId, false);
        return $meal;
    }

    /*
     * 套餐列表接口根据商家id
     * **/
    public static function mealListByShopId($shopId)
    {
        $mealList = self::getMealInfoCacheByShopId($shopId);
        $mealLists = self::formatActivityMeal($mealList);
        $mealIds = array_column($mealLists, 'meal_id');

        $mealInfos = self::getMealInfoCacheByMealIds($mealIds);

        foreach ($mealLists as $mealId => $list) {
            $lists[] = self::formatMealInfo([$mealId => $list], $mealInfos[$list['meal_id']]);
        }

        return $lists ? array_filter($lists): [];
    }

    /*
     * 套餐详情接口
     * **/
    public static function mealInfoByMealId($mealId, $type)
    {
        // 查询基础信息
        $mealList = cMeal::getItem($mealId);
        if($mealList === false) {
            $mealList = mActivityMeal::getMealInfoByMealId($mealId, self::$mealField);
            $expire =  $mealList ? strtotime($mealList['end_time']) - time() : cMeal::_CACHE_EXPIRE_TIME;
            cMeal::setItem($mealId, $mealList, $expire);
        }
        if(!$mealList) return (object)[];

        mActivityMeal::formatActivityMeal($mealList);

        if($type == self::TYPE_H5) {

            return $mealList ?  : [];

        } elseif ($type == self::TYPE_APP || $type == self::TYPE_ALL) {

            $mealId = $mealList['meal_id'];
            $mealInfo = self::getMealInfoCacheByMealId($mealId);

            $list = self::formatMealInfo([$mealId => $mealList], $mealInfo, true);
            if(!empty($list)) {
                $shopInfos = sShopInfo::getShopInfoList([$mealList['shop_id']], 'app'); // 商家信息
                $list['shop_info'] = self::formatShopInfo($shopInfos[$mealList['shop_id']]);
                $list['share_info'] = sShare::getMealShare($mealList);
                $list['meal_url'] = tBizUrl::h5MealDetail($mealId); // 套餐的图文详情link
                $list['meal_info_url'] = tBizUrl::h5MealInfo($mealId); // 套餐详情link
            }
            return $list ? $list : (object)[];
        } else {
            throw new \Exception('请求参数异常');
        }
    }

    /*
     * 替换商品列表
     * **/
    public static function replaceMealList($mealId, $group, $replaceGoodsId, $replaceSkuId)
    {
        $mealInfos = self::getMealInfoCacheByMealId($mealId);

        foreach ($mealInfos as $info)
        {
            if($info['group'] == $group) {
                $mealInfo[] = $info;
            }
        }

        $mealInfo = array_column($mealInfo, null, 'goods_id');
        $goodsIds = array_column($mealInfo, 'goods_id');

        $goodsInfo = sGoods::Goods($goodsIds);
        foreach ($mealInfo as $goodsId => &$info) {

            if ($goodsInfo[$goodsId]['status'] == mGoodsBasicInfo::STATUS_APP_NOMAL && $goodsInfo[$goodsId]['actual_stock'] > 0) {
                if($goodsId == $replaceGoodsId) {
                    $skuInfo = sSku::Sku4Order([$replaceSkuId]);
                    $skuInfo = self::checkSkuStatus($goodsId, $replaceSkuId, $skuInfo, false);
                }

                if(is_null($skuInfo) || $skuInfo == false) {
                    $skuInfo = sSku::Sku4Order([$mealInfo[$goodsId]['sku_id']]);
                    $skuInfo = self::checkSkuStatus($goodsId, $mealInfo[$goodsId]['sku_id'], $skuInfo);
                }

                $skuId = array_keys($skuInfo);
                if (is_array($skuId) && !empty($skuId)) {
                    $skuId = array_shift($skuId);
                }

                $info['sku_id'] = $skuId;
                $info['cover_img'] = $skuInfo[$skuId]['cover_img'] ?: $skuInfo[$skuId]['goods_info']['cover_img'];
                $info['title'] = $skuInfo[$skuId]['goods_info']['title'];
                $info['sku_desc'] = $skuInfo[$skuId]['sku_desc'];
                $info['ori_price'] = $skuInfo[$skuId]['ori_price'];
                $info['actual_price'] = $skuInfo[$skuId]['actual_price'];
                $info['status'] = $skuInfo[$skuId]['status'];
                $info['actual_stock'] = $skuInfo[$skuId]['actual_stock'];
                $info['sku_tip'] = self::checksSkuStock($skuInfo[$skuId]['actual_stock']);
                $info['sku_params']['t1'] = $skuInfo[$skuId]['sku_attr_t1']['id'];
                if(!empty($skuInfo[$skuId]['sku_attr_t2'])) {
                    $info['sku_params']['t2'] = $skuInfo[$skuId]['sku_attr_t2']['id'];
                }

            } else {
                if($goodsInfo[$goodsId]['status'] == mGoodsBasicInfo::STATUS_APP_OFFLINE || $goodsInfo[$goodsId]['actual_stock'] <= 0) {
                    $invalidGoods[] = $goodsId;
                }
                unset($mealInfo[$goodsId]);
            }
            unset($skuInfo);
        }

        if (!empty($invalidGoods)) {
            $invalidGoods = self::formatInvalidGoods($invalidGoods, $goodsInfo);
            $mealInfo = array_merge($mealInfo, $invalidGoods);
        }

        return $mealInfo ? array_values($mealInfo) : [];
    }

    /*
     * 套餐列表接口根据商品id
     * **/
    public static function mealListByGoodsId($goodsId)
    {
        $goodsMealInfo = self::getMealInfoCacheByGoodsId($goodsId, self::CACHE_TYPE_GOODS);
        if(!empty($goodsMealInfo)) {
            $mealIds = array_column($goodsMealInfo, 'meal_id');
            $meal = self::getMealCacheByMealIds($mealIds);
            $mealList = self::formatActivityMeal($meal);
            $mealIds = array_column($mealList, 'meal_id');
            $mealInfo = self::getMealInfoCacheByMealIds($mealIds);
            foreach ($mealList as $mealId => $list) {
                $lists[] = self::formatMealInfo([$mealId => $list], $mealInfo[$list['meal_id']]);
            }
        }

        return $lists ? array_filter($lists) : [];
    }

    /*
     * 商品详情获取套餐信息
     * **/
    public static function getMealInfoByGoodsId($goodsId)
    {
        $goodsMealInfo = self::getMealInfoCacheByGoodsId($goodsId, self::CACHE_TYPE_GOODS);
        $mealListInfo=[];
        if(!empty($goodsMealInfo)) {
            $mealIds = array_column($goodsMealInfo, 'meal_id');
            $meal = self::getMealCacheByMealIds($mealIds);
            if(array_filter($meal)) {
                $mealList = self::formatActivityMeal($meal);
                $mealInfos = self::getMealInfoCacheByMealIds($mealIds);
                $mealListInfo  = self::formatMealGoods($mealInfos, $mealList, $goodsId);
            }
        }

        return $mealListInfo ? array_filter($mealListInfo) : [];
    }

    /*
     * 购物车获取套餐信息
     * **/
    public static function getMealInfoByGoodsIds($goodsIds)
    {
        if(empty($goodsIds)) {
            return [];
        }

        foreach ($goodsIds as $goodsId)
        {
            $goodsMealInfo = self::getMealInfoCacheByGoodsId($goodsId, self::CACHE_TYPE_GOODS);
            $goodsMealInfos[] = $goodsMealInfo;
        }

        if(!empty(array_filter($goodsMealInfos))) {
            foreach ($goodsMealInfos as $info)
            {
                foreach ($info as $item)
                {
                    $goodsMeal[] = $item;
                }
            }

            $goodsMeal = array_column($goodsMeal, null, 'goods_id');
            $mealIds = array_column($goodsMeal, 'meal_id');

            $meal = self::getMealCacheByMealIds($mealIds);

            if(array_filter($meal)) {
                $mealList = self::formatActivityMeal($meal);
                $mealInfos = self::getMealInfoCacheByMealIds($mealIds);
                $mealListInfo = self::formatMealGoods($mealInfos, $mealList);
                $mealLists = array_column($mealListInfo, null, 'meal_id');

                // 将正常的数据返回
                $goodsInfo = [];
                foreach ($goodsMeal as $goodsId => $info)
                {
                    if(isset($mealLists[$info['meal_id']]) &&!empty($mealLists[$info['meal_id']])) {
                        $goodsInfo[$goodsId]['meal_id'] = $meal[$info['meal_id']]['meal_id'];
                        $goodsInfo[$goodsId]['meal_desc'] = '此商品正在套餐热卖，马上抢购';
                        $goodsInfo[$goodsId]['meal_link'] = tLink::format(tLink::LINK_H5, tBizUrl::h5MealListInfo($goodsId));
                    }
                }
            }
        }

        return $goodsInfo ? array_filter($goodsInfo) : [];
    }

    /*
     * 根据套餐id获取对应的折扣,并返回计算总价所需要的数据结构
     * **/
    public static function getMealRebateAndPriceByMealId($mealId, $skuIds)
    {
        $mealInfo = self::getMealInfoCacheByMealId($mealId);
        $mealInfo = array_column($mealInfo ,null, 'goods_id');
        $skuInfo = sSku::Sku4Order($skuIds);
        $skuInfo = array_column($skuInfo, null, 'goods_id');

        foreach ($mealInfo as $goodsId => $info)
        {
            if(isset($skuInfo[$goodsId]) && !empty($skuInfo[$goodsId])) {
                $mealRebate[$goodsId]['sku_id'] = $skuInfo[$goodsId]['sku_id'];
                $mealRebate[$goodsId]['ori_price'] = $skuInfo[$goodsId]['ori_price'];
                $mealRebate[$goodsId]['meal_rebate'] = $info['meal_rebate'];

            }
        }
        return $mealRebate;
    }

    /*
     * [0 => 'sku_id' => '1', 'meal_rebate' => '0.5', 'ori_price' =>100, 1 => 'sku_id' => '2', 'meal_rebate' => '0.5', 'ori_price' =>100]
     * **/
    public static function computeMealPrice($mealPriceInfo, $isAllSkuPrice = false)
    {
        $mealPrice = 0;
        $oriPrice = 0;
        $allSkuPrice = [];
        foreach ($mealPriceInfo as $info) {
            $oriPrice += $info['ori_price'];// 总的原价
            $mealPrice += round(($info['meal_rebate'] * $info['ori_price']) / 10); // 总的套餐价

            $allSkuPrice[] = [
                'sku_id' => $info['sku_id'],
                'price' => round(($info['ori_price'] * $info['meal_rebate']) / 10)
            ];
        }

        if ($isAllSkuPrice === true) {
            $price['all_sku_price'] = $allSkuPrice;
        }

        $price['retrench_price'] =  (string) ($oriPrice - $mealPrice);
        $price['total_price'] = (string) $mealPrice;

        return $price;
    }

    /**
     * 获取套餐优惠总价和商品套餐优惠价格
     * @param $mealId 套餐id
     * @param $goodsList ['goods_id1'=>[['sku_id'=>11,'ori_price'=>22],['sku_id'=>12,'ori_price'=>93]],'goods_id2'=>[['sku_id'=>61,'ori_price'=>82],['sku_id'=>62,'ori_price'=>103]]]
     * @return array
     */
    public static function getMealGoodsPrice($mealId, $goodsList = [])
    {
        if (!$goodsList) {
            return false;
        }
        $mealInfo = self::getMealInfoCacheByMealId($mealId);
        $result = [];
        if ($mealInfo) {
            $mealGoodsIds = array_column($mealInfo, 'goods_id');
            $mealInfo = array_column($mealInfo, null,'goods_id');
            $goodsIds = array_keys($goodsList);
            //判断goodsIds是否在mealInfo中
            if (!empty(array_diff($goodsIds, $mealGoodsIds)) || !$goodsIds) {
                return false;
            }

            //计算sku的套餐优惠价格
            $allOriPrice = 0;
            $allMealPrice = 0;
            foreach ($goodsList as $goodsId => $skuList) {
                foreach ($skuList as $row) {
                    $skuId = $row['sku_id'];
                    $oriPrice = $row['ori_price'];
                    $price = round(($mealInfo[$goodsId]['meal_rebate'] * $row['ori_price']) / 10);
                    $result['sku_price'][$skuId] = [
                        'total_price' => $price,//优惠后金额
                        'detract_price' => $oriPrice - $price,//优惠金额
                    ];
                    $allOriPrice += $row['ori_price'];// 总的原价
                    $allMealPrice += round(($row['ori_price'] * $mealInfo[$goodsId]['meal_rebate']) / 10); // 总的套餐价
                }
            }

            //计算套餐总的优惠价格
            $result['total_price'] = (string) $allMealPrice;//套餐优惠后的金额
            $result['detract_amount'] = (string) ($allOriPrice - $allMealPrice);//套餐优惠的总金额
        }
        return $result;
    }

    /*
     * 找出所有失效的商品
     * **/
    private static function allInvalidGoods($allGroup, $vaildGroup, $invalidGoods, $goodsInfo)
    {
        if (is_null($vaildGroup) || is_null($invalidGoods)) {
            return [];
        }
        foreach ($allGroup as $group) {
            if (!in_array($group, $vaildGroup)) {
                $allInvalidGroup[] = $group;
            }
        }

        if($allInvalidGroup) {
            foreach ($allInvalidGroup as $invalidGroup) {
                $allInvalidGoods[] = array_shift($invalidGoods[$invalidGroup]);
            }
            $list = self::formatInvalidGoods($allInvalidGoods, $goodsInfo);
        }


        return $list ? : [];
    }

    /*
     * 构造失效商品的结构
     * **/
    private static function formatInvalidGoods($allInvalidGoods, $goodsInfo)
    {
        foreach ($allInvalidGoods as $goodsId) {
            if(!is_null($goodsInfo[$goodsId]['status'])) {
                $info[$goodsId]['goods_id'] = $goodsId;
                $info[$goodsId]['cover_img'] = $goodsInfo[$goodsId]['cover_img'];
                $info[$goodsId]['title'] = $goodsInfo[$goodsId]['title'];
                $info[$goodsId]['status'] = mGoodsBasicInfo::STATUS_APP_OFFLINE;
            }
        }
        return $info ? array_values($info) : [];
    }

    /*
     * 实时监测sku
     * **/
    private static function checkSkuStatus($goodsId, $skuId, $skuInfo, $isReturnInfo = true)
    {
        if ($skuInfo[$skuId]['status'] != mGoodsSku::STATUS_PASSED || $skuInfo[$skuId]['actual_stock'] <= 0) {
            if(!$isReturnInfo) {
                return false;
            }
            $skuInfo = sSku::Sku($goodsId, sSku::FIELDS_GROUP_SKU_INFO);
            foreach ($skuInfo as $info)
            {
                if ($info['sku_id'] != $skuId && $info['stock'] > 0) {
                    $skuInfo = sSku::Sku4Order([$info['sku_id']]);
                    break;
                }
            }
        }

        return $skuInfo;
    }

    /*
     * 检测库存是否充足
     * **/
    private static function checksSkuStock($skuActualStock)
    {
        $skuTip = [
            'icon' => '',
            'text' => '',
        ];
        if($skuActualStock <= 5) {
            $skuTip = [
                'icon' => '',
                'text' => '库存紧张'
            ];
        }
        return $skuTip;
    }

    private static function formatMealInfo($mealList, $mealInfo, $isComputePrice = false)
    {
        $result = [];
        $mealPrice = [];
        $goodsIds = array_column($mealInfo, 'goods_id');
        $goodsInfo = sGoods::Goods($goodsIds);
        $allGroup = array_unique(array_column($mealInfo, 'group')); // 记录所有的有效id

        // 检测spu维度
        foreach ($mealInfo as $info) {
            if ($goodsInfo[$info['goods_id']]['status'] == mGoodsBasicInfo::STATUS_APP_NOMAL && $goodsInfo[$info['goods_id']]['actual_stock'] > 0) {
                $data[$info['meal_id']]['meal_info'][$info['group']][] = $info;
                $vaildGroup[] = $info['group'];
            } else if($goodsInfo[$info['goods_id']]['status'] == mGoodsBasicInfo::STATUS_APP_OFFLINE || $goodsInfo[$info['goods_id']]['actual_stock'] <= 0) {
                $invalidGoods[$info['group']][] = $info['goods_id'];
            }
            // 需要记录所有的分组,最后比对分组关联的商品是否全部失效
            $groupInfo = $data[$info['meal_id']]['meal_info'][$info['group']];

            if (count($groupInfo) > self::MIN_GROUP_COUNT && !empty($groupInfo)) {
                $list = array_shift($groupInfo);
                $mealList[$info['meal_id']]['goods_info'][$list['goods_id']] = array_merge($list, $goodsInfo[$list['goods_id']], ['is_replace' => self::SURE_REPLACE]);
            } else if (!empty($groupInfo) && count($groupInfo) < self::MAX_GROUP_COUNT) {
                $list = array_shift($groupInfo);
                $mealList[$info['meal_id']]['goods_info'][$list['goods_id']] = array_merge($list, $goodsInfo[$list['goods_id']], ['is_replace' => self::NOT_SURE_REPLACE]);
            }
        }
        $mealInfo = array_column($mealInfo, null, 'sku_id');

        // 找出完全失效的分组下的第一个商品.及基础信息
        $invalidGoodsInfo = self::allInvalidGoods($allGroup, $vaildGroup, $invalidGoods, $goodsInfo);
        foreach ($mealList as $info) {
            if($info['goods_info']) {
                foreach ($info['goods_info'] as $goodsId => &$item) {
                    $skuInfo = sSku::Sku4Order([$item['sku_id']]);
                    // 折扣为主推sku的信息,所以单独赋值
                    $skuRebate = $mealInfo[$item['sku_id']]['meal_rebate'];
                    // 实时监测主推sku是否正常,如果不正常则由其他sku替补.
                    $skuInfo = self::checkSkuStatus($goodsId, $item['sku_id'], $skuInfo);
                    $skuId = array_keys($skuInfo);
                    if (is_array($skuId) && !empty($skuId)) {
                        $item['sku_id'] = array_shift($skuId);
                    }

                    // 需要将商品头图,替换为主推sku的图片
                    if ($skuInfo[$item['sku_id']]['cover_img'] && $isComputePrice === true) {
                        $info['goods_info'][$goodsId]['cover_img'] = $skuInfo[$item['sku_id']]['cover_img'];
                    }

                    $mealRebate[] = [
                        'sku_id' => $skuInfo[$item['sku_id']]['sku_id'],
                        'meal_rebate' => $skuRebate,
                        'ori_price' => $skuInfo[$item['sku_id']]['ori_price']
                    ];

                    $info['goods_info'][$goodsId]['meal_rebate'] = $skuRebate;
                    $info['goods_info'][$goodsId]['sku_id'] = $skuInfo[$item['sku_id']]['sku_id'];
                    $info['goods_info'][$goodsId]['sku_desc'] = $skuInfo[$item['sku_id']]['sku_desc'];
                    $info['goods_info'][$goodsId]['ori_price'] = $skuInfo[$item['sku_id']]['ori_price'];
                    $info['goods_info'][$goodsId]['actual_price'] = $skuInfo[$item['sku_id']]['actual_price'];
                    $info['goods_info'][$goodsId]['actual_stock'] = $skuInfo[$item['sku_id']]['actual_stock'] ?: $skuInfo[$item['sku_id']]['stock'];
                    $info['goods_info'][$goodsId]['sku_tip'] = self::checksSkuStock($skuInfo[$item['sku_id']]['actual_stock']);

                    $info['goods_info'][$goodsId]['sku_params']['t1'] = $skuInfo[$item['sku_id']]['sku_attr_t1']['id'];
                    if(!empty($skuInfo[$item['sku_id']]['sku_attr_t2'])) {
                        $info['goods_info'][$goodsId]['sku_params']['t2'] = $skuInfo[$item['sku_id']]['sku_attr_t2']['id'];
                    }

                    unset($item['ori_min_price']);
                    unset($item['ori_max_price']);
                    unset($item['actual_min_price']);
                    unset($item['actual_max_price']);
                }

                $info['goods_info'] = array_values($info['goods_info']);
            }

            if (count($info['goods_info']) > 1) {
                $info['goods_list'] = array_merge($info['goods_info'], $invalidGoodsInfo);
                unset($info['goods_info']);
                $result = $info;
                if ($isComputePrice === true) {
                    $mealPrice = self::computeMealPrice($mealRebate);
                }
            }
        }

        $result = array_merge($result, $mealPrice);
        return $result ?: [];
    }

    /*
     * 处理通过goodsid调用套餐的数据
     * **/
    private static function formatMealGoods($mealInfos, $meal, $goodsId = 0)
    {
        $mealListInfo = [];
        foreach($mealInfos as $mealId => $mealInfo)
        {
            foreach($mealInfo as $item)
            {
                $groupInfos[$item['group']][] = $item;
            }

            $groupCount = 0;
            foreach($groupInfos as $group => $groupInfo)
            {
                foreach($groupInfo as $info)
                {
                    if($groupCount < 2) {
                        $goods = sGoods::Goods($info['goods_id']);
                        if($goods['status'] == mGoodsBasicInfo::STATUS_APP_NOMAL && $goods['actual_stock'] > 0) {
                            $groupCount++;
                            break;
                        }
                    }

                }
            }
            if($groupCount == 2) {
                $mealListInfo[] = isset($meal[$mealId]) ? [
                    'meal_id' => $meal[$mealId]['meal_id'],
                    'cover_img' => $meal[$mealId]['cover_img'],
                    'meal_desc' => $meal[$mealId]['meal_desc'],
                    'meal_link' => tLink::format(tLink::LINK_H5, tBizUrl::h5MealListInfo($goodsId)),
                ] : [];
            }
            $groupCount = 0;
        }

        return $mealListInfo;
    }

    private static function formatActivityMeal($mealLists)
    {
        $mealList = [];
        foreach ($mealLists as $info)
        {
            mActivityMeal::formatActivityMeal($info);
            $mealList[$info['meal_id']] = $info;
        }

        return $mealList;
    }

    /*
     * 套餐详情所需要的扩展信息
     * **/
    private static function getExtendInfo($activityMeal)
    {
        foreach ($activityMeal['meal_info'] as $group => &$infos) {
            $goodsInfo = array_column($infos, null, 'goods_id');
            $goodsIds = array_column($infos, 'goods_id');

            foreach ($goodsIds as $goodsId) {

                //查询商品spu信息
                $info = mGoodsBasicInfo::getInfo($goodsId, '*');
                $goodsInfo[$goodsId]['title'] = $info['title'];
                $goodsInfo[$goodsId]['min_price'] = $info['min_price'];
                $goodsInfo[$goodsId]['max_price'] = $info['max_price'];
                $goodsInfo[$goodsId]['spec_img_thumb_url'] = $info['spec_img_thumb_url'];
                // 下拉框所需信息
                $skuInfo = mGoodsSku::getSkuInfo([$goodsId], true, false);
                $skuInfo = array_shift($skuInfo);
                $goodsInfo[$goodsId]['sku']['list'] = $skuInfo['list'];

                $infos = $goodsInfo;
            }
            $infos = array_values($infos);
        }

        return $activityMeal;
    }

    private static function getExtendInfoList(&$info)
    {
        foreach ($info['meal_info'] as $item) {
            $info['goods_count'] += count($item);
            $goodsInfo = array_shift($item);
            $skuInfo = sSku::Sku4Order([$goodsInfo['sku_id']]);
            $info['price'] += $skuInfo[$goodsInfo['sku_id']]['ori_price'];
            $info['meal_price'] += round(($skuInfo[$goodsInfo['sku_id']]['ori_price'] * $goodsInfo['meal_rebate'])  / 10 );
            $info['main_push_goods_count']++;
        }
    }

    private static function mgtFormatMealList($activityList)
    {
        $list = [];
        foreach ($activityList as $info) {
            $list[$info['meal_id']] = [
                'meal_id' => $info['meal_id'],
                'meal_title' => $info['meal_title'],
                'cover_img' => $info['cover_img']['url'],
                'sort' => $info['sort'],
                'status' => $info['status'],
                'status_desc' => $info['status_desc'],
                'goods_count' => $info['goods_count'],
                'price' => (string)$info['price'],
                'meal_price' => (string)$info['meal_price'],
                'main_push_goods_count' => $info['main_push_goods_count']
            ];
        }

        return $list;
    }

    private static function formatShopInfo($shopInfos)
    {
        $shopInfo = [
            'cover_img' => $shopInfos['shop_imgurl'],
            'shop_id' => $shopInfos['shop_id'],
            'type' => $shopInfos['type'],
            'shop_name' => $shopInfos['shop_name'],
            'score' => $shopInfos['score'],
            'status' => $shopInfos['status'],
            'se_group_id' => $shopInfos['small_energy']['pre_sale_reception_group_id']
        ];
        return $shopInfo;
    }

}